package cn.senwill.product_service.service.impl;

import cn.senwill.product_service.domain.Product;
import cn.senwill.product_service.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import java.util.*;

@RefreshScope// 当配置中心更新改变时，局部刷新该区域
@Service
public class ProductServiceImpl implements ProductService {

    @Value("${server.port}")
    private String port;

//    @Value("${zhiquan}")
//    private String zhiquan;

//    @Value("${branch}")
//    private String branch;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private static final Map<Integer, Product> productMap = new HashMap<>();

    static {
        Product product1 = new Product(1, "iPhone 13", 9.9, 100, "0");
        Product product2 = new Product(2, "冰箱", 3.9, 100, "0");
        Product product3 = new Product(3, "洗衣机", 2.9, 100, "0");
        Product product4 = new Product(4, "空调", 1.9, 100,"0");
        Product product5 = new Product(5, "蓝牙耳机", 7.9, 100,"0");
        Product product6 = new Product(6, "电脑", 6.9, 100,"0");
        Product product7 = new Product(7, "鼠标", 6.9, 100,"0");

        productMap.put(product1.getId(), product1);
        productMap.put(product2.getId(), product2);
        productMap.put(product3.getId(), product3);
        productMap.put(product4.getId(), product4);
        productMap.put(product5.getId(), product5);
        productMap.put(product6.getId(), product6);
        productMap.put(product7.getId(), product7);
    }

    @Override
    public List<Product> getList() {
        Collection<Product> products = productMap.values();
        List<Product> productList = new ArrayList<>(products);
        return productList;
    }

    @Override
    public Product findById(Integer id) {
        Product product = productMap.get(id);
        Product p = new Product();
        p.setId(product.getId());
        p.setName(product.getName());
        p.setPrice(product.getPrice());
        p.setStore(product.getStore());
        p.setPort(port);
//        p.setPort(port + " zhiquan=> " + zhiquan);
//        p.setPort(port + " env=> " + env + " branch=> " + branch);
        return p;
    }

}
